/* Copyright (C) 2007 Ryan Capote
 * This file is apart of BlueEngine
 *  See Licsense.txt
 */

#include "config.h"

namespace BlueEngine
{

    namespace Utilities
    {

         /*
          * Set the config file to use
          */
         void Config::setConfigFile( const std::string& filename )
         {
              m_file = filename;
         }

         /*
          * Load options from config file
          */
         bool Config::loadConfig()
         {
              // We need to specify the file
              // via setConfigFile()
              if ( m_file == "" )
                   return false;

              if( !m_doc.LoadFile( m_file.c_str() ) )
                   return false;

              TiXmlNode *parent = 0;
              TiXmlNode *child = 0;

              parent = m_doc.FirstChild( "config" );

              while( child = parent->IterateChildren( child ) ) 
              {
                   TiXmlElement *element = child->ToElement();
                   m_optionlist[element->Attribute( "name" )] = 
                                element->Attribute( "value");
              }

              return true;
	 } 

         /*
	  *Save options to config file
	  */
         void Config::saveConfig()
         {
              // We need to specify the file
              // via setConfigFile()
              if( m_file == "" )
                   return;

              std::string config;
              
	      config.append( "<?xml version='1.0' encoding='UTF-8'?>" );
              config.append( "<config>" ); 

              OptionList::iterator iter;

              for( iter = m_optionlist.begin(); 
                   iter != m_optionlist.end(); 
                   ++iter )
              {
                  config.append( "<option name='" );
                  config.append( iter->first );
                  config.append( "' value='" );
                  config.append( iter->second );
                  config.append( "' />" );
              }

              config.append( "</config>" );

              TiXmlDocument doc( m_file.c_str() );
              doc.Parse( config.c_str() );

              doc.SaveFile();
         }
		
         /*
          * Set the value of an option
          */
         void Config::setOption( const std::string& name, 
                                 const std::string& value )
         {
              m_optionlist[name] = value;
         }

         /*
          * Get value of an option
          */
         std::string Config::getOption( const std::string& name )
         {
              OptionList::const_iterator iter;

              for( iter = m_optionlist.begin(); 
                   iter != m_optionlist.end(); 
                   ++iter )
              {
                   if( iter->first == name )
                        return iter->second;
              }

              return "";
         }

    } 

}

